package com.minshenglife.zanzan.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.IntegralDao;
import com.minshenglife.zanzan.entity.Integral;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.pub.Keys;
import com.minshenglife.zanzan.service.IntegralService;


/**
 *积分流水service
 * @author huodd
 *
 */
@Component
@Transactional
public class IntegralServiceImpl implements IntegralService {

	@Resource
	IntegralDao integralDao;
	
	@Override
	public void add(Integral inte) {
		inte.setState(Keys.INTEGRAL_STATE_ZERO);
		integralDao.add(inte);
	}

	@Override
	public Long getAddIntegralCountOfTerm(String empId, Date startDate,
			Date endDate,String integralType) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(integralType!=null && !"".equals(integralType))
			map.put("integralType", integralType);
		map.put("integralManId", empId);
		map.put("state", Keys.INTEGRAL_STATE_ZERO);
		map.put("notEqSendManId", Keys.SYSTEM_SUPER_USER);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		Long num=integralDao.integralTotalNum(map);
		return num;
	}

	@Override
	public Page<Integral> getAddIntegralInfoOfTerm(String empId,
			Date startDate, Date endDate, int pageNo, int pageSize,String integralType) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(integralType!=null && !"".equals(integralType))
			map.put("integralType", integralType);
		
		int offset = (pageNo-1) * pageSize;
        map.put("from", offset);
        map.put("to", offset+pageSize);
		map.put("integralManId", empId);
		map.put("state", Keys.INTEGRAL_STATE_ZERO);
		map.put("notEqSendManId", Keys.SYSTEM_SUPER_USER);
		SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("startDate", sdf.format(startDate));
		map.put("endDate", sdf.format(endDate));
		Long count=integralDao.count(map);
		List<Integral> rows =integralDao.findBy(map);
        Page<Integral> ret = new Page<Integral>();
        ret.setRows(rows);
        ret.setTotalRow(count);
        ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
        ret.setPageSize(pageSize);
        ret.setPageNo(pageNo);
		return ret;
	}
	
	

}
